package com.gouzheng.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gouzheng.entity.Paper;
import com.gouzheng.entity.Topic;
import com.gouzheng.entity.User;
import com.gouzheng.service.paperService;
import com.gouzheng.util.BaseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/paper")
public class paperController {

    @Autowired
    private paperService paperService;

//    //查看全部考卷
//    @RequestMapping("/getPaperAll")
//    public String getPaperAll(Model model,String paperName){
//        List<Paper> paperList = paperService.getPaperAll(paperName);
//        model.addAttribute("list",paperList);
//        return "paper";
//    }

    //查看考题
    @RequestMapping("/getTopic")
    public String getTopic(Model model,Integer paperId,String pName){
        List<Topic> topicList = paperService.getTopic(paperId);
        model.addAttribute("list",topicList);
        model.addAttribute("pName",pName);
        model.addAttribute("paperId",paperId);
        return "topic";

    }

    //跳转到修改页面
    @RequestMapping("/toupdateTopic")
    public String selectTopic(Model model,Integer paperId,Integer titleNumber){
        Topic topic = paperService.selectTopic(paperId, titleNumber);
        model.addAttribute("list",topic);
        return "updateTopic";
    }

    //修改考题
    @RequestMapping("/updateTopic")
    public String updateTopic(Topic topic, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes){
        paperService.updateTopic(topic);
        redirectAttributes.addAttribute("paperId",topic.getPaperId());
        return "redirect:/paper/getTopic";
    }
    //删除考题
    @RequestMapping("/deleteTopic/{paperId}/{titleNumber}")
    public String deleteTopic(@PathVariable("paperId") Integer paperId, @PathVariable("titleNumber") Integer titleNumber,RedirectAttributes redirectAttributes){
        paperService.deleteTopic(paperId,titleNumber);
        redirectAttributes.addAttribute("paperId",paperId);
        return "redirect:/paper/getTopic";
    }


    //跳转到答题页面
    @RequestMapping("/upLinex")
    public String upLinex(Model model,Integer delteFlag){
        List<Paper> paperAll = paperService.getStu(1);
        model.addAttribute("list",paperAll);
        if(paperAll==null){
            return "stuRemark";
        }
        return "voluem";
    }



    //添加考卷
    @RequestMapping("/addPaper")
    @ResponseBody
    public BaseBean addPaper(Paper paper, HttpSession session){
        BaseBean baseBean=new BaseBean();
        //从session中取出用户id
        User user = (User) session.getAttribute("user");
        //存入对象
        paper.setId(user.getId());
        //执行添加
        paperService.addPaper(paper);
        //返回
        return baseBean;
    }

    //添加考题
    @RequestMapping("/addTopic")
    @ResponseBody
    public BaseBean addTopic(Topic topic){
        BaseBean baseBean=new BaseBean();
        //查询数量
        Integer countTopic = paperService.findCountTopic(topic.getPaperId());
        //查询题量
        Paper paper = paperService.findId(topic.getPaperId());

        //判断 如果已有题量超过总体量则不添加
        if(countTopic >= paper.getPaperNumber()){
            baseBean.setMsg("已添加至最高设置的题量");
            return baseBean;
        }

        if(topic.getA() != null){
            topic.setA("A、"+topic.getA());
        }
        if(topic.getB() != null){
            topic.setB("B、"+topic.getB());
        }
        if(topic.getC() != null){
            topic.setC("C、"+topic.getC());
        }
        if(topic.getD() != null){
            topic.setD("D、"+topic.getD());
        }

        topic.setTitleNumber(countTopic+1);

        //执行
        paperService.addTopic(topic);
        baseBean.setMsg("创建成功");
        //返回
        return baseBean;
    }

    //查询全部试卷并进行分页
    @RequestMapping("/pages")
    public String pages(Integer pageNum,HttpServletRequest request,String paperName){
        try{
            if(pageNum==null){
                pageNum=1;
            }else {
                pageNum = pageNum;
            }
            //每页展示的条数
            PageHelper.startPage(pageNum,4);
            //查询全部考卷
            List<Paper> paperList = paperService.getPaperAll(paperName);
            //循环遍历出来全部试卷
            for(Paper paper : paperList){
                System.out.println(paper);
            }
            //将试卷展示到页面
            request.setAttribute("lists",paperList);
            PageInfo<Paper> pageInfo=new PageInfo<Paper>(paperList);
            System.out.println("当前页"+pageInfo.getPageNum());
            System.out.println("每页显示条数"+pageInfo.getPageSize());
            System.out.println("总条数"+pageInfo.getTotal());
            System.out.println("总页数"+pageInfo.getPages());
            System.out.println("上一页"+pageInfo.getPrePage());
            System.out.println("下一页"+pageInfo.getNextPage());
            System.out.println("是否是第一页"+pageInfo.isIsFirstPage());
            System.out.println("是否是最后页"+pageInfo.isIsLastPage());
            //将分页展示到页面上
            request.setAttribute("pages",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "paper";
    }

    //上线下线
    @RequestMapping("/linePaper")
    @ResponseBody
    public  BaseBean linePaper(Integer paperId){
        BaseBean baseBean=new BaseBean();
        //根据id查出对象
        Paper paper = paperService.findId(paperId);
        //如果是考试题要上线判断题量是否足够
        if(paper.getPaperType()==1 && paper.getDeleteFlag()==1){
            //判断是否可以上线
            Integer countTopic = paperService.findCountTopic(paperId);
            if(countTopic<paper.getPaperNumber()){
                baseBean.setMsg("未达到设置考题的数量");
                return baseBean;
            }else{
                paperService.linePaper(2,paperId);
                baseBean.setMsg("已下线");
            }

        }
        if(paper.getPaperType()==1 && paper.getDeleteFlag()==2){
            //判断是否可以上线
            Integer countTopic = paperService.findCountTopic(paperId);
            if(countTopic<paper.getPaperNumber()){
                baseBean.setMsg("未达到设置考题的数量");
                return baseBean;
            }else{
                paperService.linePaper(1,paperId);
                baseBean.setMsg("已上线");
            }

        }
        //上线
        if(paper.getPaperType()==1 && paper.getDeleteFlag()==2){
            paperService.linePaper(1,paperId);
            baseBean.setMsg("已上线");
            //上线
        }
        if(paper.getPaperType()==0 && paper.getDeleteFlag()==2){
            paperService.linePaper(1,paperId);
            baseBean.setMsg("已上线");
            //下线
        }
        if(paper.getPaperType()==0 && paper.getDeleteFlag()==1){
            paperService.linePaper(2,paperId);
            baseBean.setMsg("已下线");
            //下线
        }
        if(paper.getPaperType()==1 && paper.getDeleteFlag()==0){
            paperService.linePaper(2,paperId);
            baseBean.setMsg("已下线");
        }

        return baseBean;
    }

//    @RequestMapping("/deletePaper")
//
//    public String deletePaper(Integer paperId){
//        paperService.deletePaper(paperId);
//        return "redirect:/paper/getPaperAll";
//    }


}
